package com.ambitor.ipro.actions;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.sql.dao.impl.SuperDaoImpl;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.ambitor.ipro.app.sche.InstallerHelper;
import com.ambitor.ipro.exceptions.ProException;
import com.ambitor.ipro.service.TopService;
import com.ambitor.ipro.util.Constants;
import com.ambitor.ipro.util.StringUtil;
import com.taobao.api.domain.Item;

public class PreviewAction extends BaseAction {

	private static final long serialVersionUID = -1641837048613839690L;

	private String result = "";

	private transient Map<String, String> userStyle = new HashMap<String, String>();

	private transient Map<String, String> userPro = new HashMap<String, String>();

	private transient String itemsStr = new String();

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getItemsStr() {
		return itemsStr;
	}

	public void setItemsStr(String itemsStr) {
		this.itemsStr = itemsStr;
	}

	public Map<String, String> getUserStyle() {
		return userStyle;
	}

	public void setUserStyle(Map<String, String> userStyle) {
		this.userStyle = userStyle;
	}

	public Map<String, String> getUserPro() {
		return userPro;
	}

	public void setUserPro(Map<String, String> userPro) {
		this.userPro = userPro;
	}

	@SuppressWarnings("unchecked")
	public String preview() {
		SuperDao superDao = null;
		try {
			superDao = new SuperDaoImpl(pd.getConn());
		} catch (SQLException e) {
			// TODO
		}
		IData userStyleMap = new DataMap(userStyle);
		String keyWords = userStyle.get("KEY_WORDS");
		StringBuilder sb = new StringBuilder();
		if (StringUtils.isNotBlank(keyWords)) {
			String[] keyWordArray = keyWords.split("[\\s]+");
			for (String kw : keyWordArray) {
				sb.append(kw);
				sb.append(",");
			}
		}
		keyWords = StringUtil.trimSuffix(sb.toString(), ",");
		userStyleMap.put("KEY_WORDS", keyWords);

		TopService topService = new TopService(Constants.EVN_SWITCH,
				Constants.APP_KEY_IPROMOTION, Constants.APP_SECRET_IPROMOTION);
		InstallerHelper installerHelper = new InstallerHelper(superDao,
				topService, pd.getUserSession(), new DataMap(userPro),
				userStyleMap);

		IDataset items = new DatasetList(itemsStr);
		List<Item> allItemListToBePromoted = new ArrayList<Item>();
		for (int i = 0; i < items.size(); i++) {
			IData data = null;
			try {
				data = items.getData(i);
			} catch (Exception e) {
			}
			Item item = new Item();
			item.setNumIid(new Long(data.getString("iid")));
			item.setPrice(data.getString("price"));
			item.setTitle(data.getString("title"));
			item.setPicUrl(data.getString("picUrl"));
			allItemListToBePromoted.add(item);
		}
		try {
			result = installerHelper.buildPromHTML(allItemListToBePromoted,
					new String[] { "" }).get(0);
		} catch (ProException e) {
			logger.warn("预览出现错误", e);
			result = "预览出现错误，请联系潇洒哥8858";
		}
		return SUCCESS;
	}
}
